package com.disapand.mall.annotation.aspect;

import com.disapand.mall.annotation.CustomAnnotation;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

import java.lang.reflect.Method;

/**
 * @author disapand
 */
@Aspect
@Component
public class CustomAspect {

    @Pointcut("@annotation(com.disapand.mall.annotation.CustomAnnotation)")
    public void annotationPointcut() {}

    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CustomAnnotation annotation = method.getAnnotation(CustomAnnotation.class);
        String value = annotation.value();
        System.out.println("准备" + value);
    }

    @After("annotationPointcut()")
    public void afterPointcut(JoinPoint joinPoint) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        Method method = methodSignature.getMethod();
        CustomAnnotation annotation = method.getAnnotation(CustomAnnotation.class);
        String value = annotation.value();
        System.out.println("完成" + value);
    }
}
